package com.prs.crm.action.oa;

import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.context.AppContext;
import com.prs.crm.domain.oa.AnnouncementCollect;
import com.prs.crm.domain.oa.AnnouncementCollectType;
import com.prs.crm.service.oa.AnnouncementCollectService;
import com.prs.crm.service.oa.AnnouncementCollectTypeService;

public class AnnouncementCollectAction extends BaseAction{
	private static final long serialVersionUID = 1L;
	private PaginationSupport pagination;
	private AnnouncementCollect announcementCollect;
	private AnnouncementCollectService announcementCollectService;
	private Integer[] collectIds;
	private AnnouncementCollectType announcementCollectType;
	private AnnouncementCollectTypeService announcementCollectTypeService;
	private Integer[] typeIds;
	private List<AnnouncementCollectType> announcementCollectTypes;
	
	@Action("listAnnouncementCollect")
	public String listAnnouncementCollect() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				AnnouncementCollect.class);
		criteria.add(Restrictions.eq("recorder.id", AppContext.getAccountContext().currentUser().getId()));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.announcementCollectService.listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	@Action("listAnnouncementCollectType")
	public String listAnnouncementCollectType() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				AnnouncementCollectType.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.announcementCollectService.listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	@Action("editAnnouncementCollectType")
	public String editAnnouncementCollectType() {
		if(this.typeIds!=null){
			this.announcementCollectType=this.announcementCollectTypeService.get(typeIds[0]);
		}
		return SUCCESS;
	}
	@Action("viewAnnouncementCollect")
	public String viewAnnouncementCollect() {
		
		return SUCCESS;
	}
	@Action(value = "removeAnnouncementCollect", results = {
			@Result(name = "success", type = "redirect", location = "listAnnouncementCollect"),
			@Result(name = "error", type = "chain", location = "listAnnouncementCollect") })
	public String removeAnnouncementCollect() {
		this.announcementCollectService.batchRemove(collectIds);
		return SUCCESS;
	}
	@Action(value = "saveAnnouncementCollectType", results = {
			@Result(name = "success", type = "redirect", location = "listAnnouncementCollectType"),
			@Result(name = "error", type = "chain", location = "editAnnouncementCollectType") })
	public String saveAnnouncementCollectType() {
		if(this.announcementCollectType.getId()==null){
			this.announcementCollectTypeService.save(announcementCollectType);
		}else{
			this.announcementCollectTypeService.submit(announcementCollectType);
		}
		return SUCCESS;
	}
	@Action(value = "removeAnnouncementCollectType", results = {
			@Result(name = "success", type = "redirect", location = "listAnnouncementCollectType"),
			@Result(name = "error", type = "chain", location = "listAnnouncementCollectType") })
	public String removeAnnouncementCollectType() {
		this.announcementCollectTypeService.batchRemove(typeIds);
		return SUCCESS;
	}
	@Action(value = "editAnnouncementMove", results = {
			@Result(name = "error", type = "chain", location = "listAnnouncementCollect") })
		public String editAnnouncementMove(){
			if (collectIds!=null) {
				this.announcementCollect=this.getAnnouncementCollectService().get(collectIds[0]);
			}else{
				this.addActionError("请选择一行进行操作！");
				return ERROR;
			}
			announcementCollectTypes=this.getAnnouncementCollectTypeService().getAll();
			return SUCCESS;
		}
		@Action(value = "saveAnnouncementMove", results = {
				@Result(name = "success", type = "redirect", location = "listAnnouncementCollect"),
				@Result(name = "error", type = "chain", location = "editAnnouncementMove") })
		public String saveAnnouncementMove(){
			if (announcementCollect==null) {
				this.addActionError("请选择一项进行移动！");
				return ERROR;
			}
			this.getAnnouncementCollectService().submit(announcementCollect);
			return SUCCESS;
		}
	public PaginationSupport getPagination() {
		return pagination;
	}
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}
	public AnnouncementCollect getAnnouncementCollect() {
		return announcementCollect;
	}
	public void setAnnouncementCollect(AnnouncementCollect announcementCollect) {
		this.announcementCollect = announcementCollect;
	}
	public AnnouncementCollectService getAnnouncementCollectService() {
		return announcementCollectService;
	}
	public void setAnnouncementCollectService(
			AnnouncementCollectService announcementCollectService) {
		this.announcementCollectService = announcementCollectService;
	}
	public Integer[] getCollectIds() {
		return collectIds;
	}
	public void setCollectIds(Integer[] collectIds) {
		this.collectIds = collectIds;
	}
	public AnnouncementCollectType getAnnouncementCollectType() {
		return announcementCollectType;
	}
	public void setAnnouncementCollectType(
			AnnouncementCollectType announcementCollectType) {
		this.announcementCollectType = announcementCollectType;
	}
	public AnnouncementCollectTypeService getAnnouncementCollectTypeService() {
		return announcementCollectTypeService;
	}
	public void setAnnouncementCollectTypeService(
			AnnouncementCollectTypeService announcementCollectTypeService) {
		this.announcementCollectTypeService = announcementCollectTypeService;
	}
	public Integer[] getTypeIds() {
		return typeIds;
	}
	public void setTypeIds(Integer[] typeIds) {
		this.typeIds = typeIds;
	}
	public List<AnnouncementCollectType> getAnnouncementCollectTypes() {
		return announcementCollectTypes;
	}
	public void setAnnouncementCollectTypes(
			List<AnnouncementCollectType> announcementCollectTypes) {
		this.announcementCollectTypes = announcementCollectTypes;
	}
	
}
